package com.bwie.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.bwie.common.domain.Log;
import com.bwie.common.domain.User;
import com.bwie.common.result.Result;
import com.bwie.user.service.UserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
@Log4j2
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private HttpServletRequest request;

    /**
     * 根据手机号查询用户
     * @param phone
     * @return
     */
    @GetMapping("/findUser/{phone}")
    public Result<User> findUser(@PathVariable String phone){
        log.info("功能：根据手机号查询用户，请求方式：{}，请求路径：{}，请求参数：{}",request.getMethod(),request.getRequestURI(),phone);
        User user = userService.findUser(phone);
        Result<User> result = Result.success(user);
        log.info("功能：根据手机号查询用户，请求方式：{}，请求路径：{}，响应结果：{}",request.getMethod(),request.getRequestURI(), JSONObject.toJSONString(phone));
        return result;
    }

    /**
     * 登录记录日志
     * @param login
     * @return
     */
    @PostMapping("/addLog")
    public Result addLog(@RequestBody Log login){
        log.info("功能：登录记录日志，请求方式：{}，请求路径：{}，请求参数：{}",request.getMethod(),request.getRequestURI(),JSONObject.toJSONString(login));
        userService.addLog(login);
        Result result = Result.success();
        log.info("功能：登录记录日志，请求方式：{}，请求路径：{}，响应结果：{}",request.getMethod(),request.getRequestURI(), JSONObject.toJSONString(result));
        return result;
    }
}
